<template>
	<div>
		<ProTable ref="proTable" title="服务协议" :columns="columns" :request-api="getTableList" :init-param="initParam" :pagination="false">
			<template #expand="scope">
				{{ scope.row }}
			</template>

			<!-- 表格操作 -->
			<template #operation="{ row }">
				<el-button
					type="primary"
					link
					v-auth-all="['system:service:agreement:details:invoke', 'system:service:agreement:update:invoke']"
					@click="onSetting(row)"
				>
					配置
				</el-button>
			</template>
		</ProTable>
		<Setting :visible="settingVisible" :row="settingRow" @handleClose="closeSetting"></Setting>
	</div>
</template>

<script setup lang="tsx" name="storeList">
import { onMounted, ref, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { NextLoading } from '/@/utils/loading';
import ProTable from '../../../../components/ProTable/index.vue';
import { ProTableInstance, ColumnProps } from '../../../../components/ProTable/interface';
import { fetchAgreement } from '/@/api/system';
import { useRouter } from 'vue-router';
import Setting from './dialog/setting.vue';

const router = useRouter();

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({
	paramCode: 'id',
});

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getTableList"
const getTableList = (params: any) => {
	let data = {
		...params,
	};
	if (data.paramValue) {
		data[data.paramCode] = data.paramValue;
	}
	delete data.paramValue;
	delete data.paramCode;

	return fetchAgreement(data);
};

// 表格配置项
const columns: ColumnProps<any>[] = [
	{
		prop: 'name',
		label: '协议名称',
		minWidth: 100,
	},
	{
		prop: 'updateTime',
		label: '更新时间',
		minWidth: 100,
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 200, align: 'left' },
];

// 配置

const settingVisible = ref(false);
const settingRow = ref();

const onSetting = (row: any) => {
	settingVisible.value = true;
	settingRow.value = row;
};

const closeSetting = (success: boolean) => {
	settingVisible.value = false;
	if (success) {
		proTable.value?.getTableList();
	}
};

// 页面加载时
onMounted(() => {
	NextLoading.done();
});
</script>

<style lang="scss">
.demo-tabs {
	.el-tabs__header {
		margin-bottom: 0;
	}
}
</style>
